package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/5/27 15:16
 * @Version 1.0
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RabbitService rabbitService;
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        //  这个交易记录中，能否保存两天一样的数据?
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOrderId,orderInfo.getId());
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getPaymentType,paymentType);
        PaymentInfo paymentInfoQuery = paymentInfoMapper.selectOne(paymentInfoLambdaQueryWrapper);
        if (null != paymentInfoQuery) return;
        //  创建paymentInfo 对象
        PaymentInfo paymentInfo = new PaymentInfo();
        //  给paymentInfo 赋值。
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setPaymentType(paymentType);
        //  tradeNo： 支付宝的交易号.
        //  paymentInfo.setTradeNo();
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        //  保存数据
        paymentInfoMapper.insert(paymentInfo);
    }

    @Override
    public void updatePaymentStatus(String outTradeNo, String paymentType, HashMap<String, String> paramsMap) {
        //  outTradeNo它可以更新. 通过 outTradeNo, paymentType.找paymentInfo;
        PaymentInfo paymentInfoQuery = this.getPaymentInfo(outTradeNo, paymentType);
        if (null == paymentInfoQuery) return;

        //  创建对象
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setTradeNo(paramsMap.get("trade_no"));
        paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(paramsMap.toString());
        //  trade_no payment_status callback_time callback_content
        //  更新交易记录 update from payment_info -- update set=? 亚男;  update payment_status=? payment_info where ;
        //  update payment_info set trade_no=? out_trade_no
        //  update payment_info set trade_no=? , payment_status=?,callback_time=?,callback_content=? where out_trade_no = ? and payment_type = ?
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo).eq(PaymentInfo::getPaymentType,paymentType);
        //  第一个参数是更改的对象 第二个参数设置更新条件
        paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);

        //  发送消息给订单.
        this.rabbitService.sendMsg(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfoQuery.getOrderId());
    }

    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String paymentType) {
        //  查询：
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo).eq(PaymentInfo::getPaymentType,paymentType);
        return paymentInfoMapper.selectOne(paymentInfoLambdaQueryWrapper);
    }

    @Override
    public void updatePaymentStatus(PaymentInfo paymentInfo, String outTradeNo, String paymentType) {
        //  构建条件
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo).eq(PaymentInfo::getPaymentType,paymentType);
        paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);
    }

    @Override
    public void paymentClose(Long orderId) {
        //  修改交易状态
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());

        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOrderId,orderId);
        paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);
    }
}
